//
//  PayVC.m
//  代理端
//
//  Created by sliver on 15/12/25.
//  Copyright (c) 2015年 sliver. All rights reserved.
//

#import "PayVC.h"
#import "Unit.h"
#import "WXDataService.h"
#import "YuEZhiFuVC.h"
#import "UPPayPlugin.h"
#import "UPPayPluginDelegate.h"
@interface PayVC ()<UPPayPluginDelegate>
{
    UITextField *telTextField;
    NSInteger flag; //支付方式
    UPPayPlugin *upPayPlugin; //银联
}
@end

@implementation PayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    flag = 1;
    upPayPlugin = [[UPPayPlugin alloc]init];
    
    [self loadNav];
    [self loadView1];
    // Do any additional setup after loading the view.
}
-(void)loadView1{
    //第一个输入框view
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 60,ScreenW, 50)];
    view1.backgroundColor = [UIColor whiteColor];
    view1.layer.masksToBounds = YES;
    view1.layer.cornerRadius = 5;
    [self.view addSubview:view1];
    //text
    UILabel *totalLab = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 50)];
    totalLab.text = @"请支付:";
    totalLab.backgroundColor = [UIColor whiteColor];
    totalLab.font = [UIFont boldSystemFontOfSize:19];
    UILabel *totalLab2 = [[UILabel alloc]initWithFrame:CGRectMake(ScreenW-240, 0, 220, 50)];
    totalLab2.text = [NSString stringWithFormat:@"%.2f元", [_totalMoey floatValue]];
    totalLab2.backgroundColor = [UIColor whiteColor];
    totalLab2.textAlignment = NSTextAlignmentRight;
    totalLab2.font = [UIFont boldSystemFontOfSize:19];
    [view1 addSubview:totalLab2];
    [view1 addSubview:totalLab];
    
     UILabel *zhifufangshiLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 125, ScreenW, 30)];
    zhifufangshiLab.text = @"   请选择支付方式:";
    zhifufangshiLab.backgroundColor = [UIColor whiteColor];
    zhifufangshiLab.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:zhifufangshiLab];
    UIView *backview = [[UIView alloc]initWithFrame:CGRectMake(0, 156, ScreenW, WITH_FOR_45 *4-WITH_FOR_5)];
    backview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backview];
    NSArray *nameAry = @[@"支付宝支付",@"微信支付",@"网银支付",@"余额支付"];
    NSArray *imgAry = @[@"zhifubao",@"weixin",@"图层-5.png",@"wallet"];

    for(int i =0;i<4;i++){
        UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(WITH_FOR_15, WITH_FOR_5 +WITH_FOR_45*i, WITH_FOR_30, WITH_FOR_30)];
        imgview.image = [UIImage imageNamed:imgAry[i]];
        imgview.backgroundColor = DRINK_LIGHTGRAY_COLOR;
        [backview addSubview:imgview];
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(WITH_FOR_50, WITH_FOR_10+WITH_FOR_45*i, WITH_FOR_100, WITH_FOR_20)];
        lab.text =nameAry[i];
        lab.font = [UIFont systemFontOfSize:17];
        [backview addSubview:lab];
        
        UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW - 30, WITH_FOR_15+WITH_FOR_45*i, 12, 12)];
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 6;
        if(i == 0){
            btn.backgroundColor = DRINK_RED_COLOR;
        }else{
            btn.backgroundColor = [UIColor grayColor];
        }
        btn.tag = i+1;
        [backview addSubview:btn];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, WITH_FOR_40+WITH_FOR_45*i, ScreenW-10, 1)];
        view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
        [backview addSubview:view];
        
        UIButton *btnn = [[UIButton alloc]initWithFrame:CGRectMake(0, WITH_FOR_45*i, ScreenW, WITH_FOR_45)];
        btnn.backgroundColor = [UIColor clearColor];
        btnn.tag = i+10;
        [btnn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        [backview addSubview:btnn];
    }
    //确认按钮
    UIButton *landBtn = [[UIButton alloc]initWithFrame:CGRectMake(60, ScreenH - 100, ScreenW - 120, 40)];
    landBtn.backgroundColor = DRINK_RED_COLOR;
    landBtn.layer.masksToBounds = YES;
    landBtn.layer.cornerRadius = 5;
    [landBtn setTitle:@"确定" forState:UIControlStateNormal];
    landBtn.titleLabel.font = [UIFont boldSystemFontOfSize:15];
    [landBtn addTarget:self action:@selector(clickLand:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:landBtn];
}
#pragma mark - 充值
-(void)clickLand:(UIButton *)senfer{
    
    switch (flag) {
        case 1:{
        }
        case 2:
//            [self payByUPPayPlugin];
            break;
        case 3:{
            [self payByUPPayPlugin];

            break;
        }
        case 4:{
            YuEZhiFuVC *yuevc = [[YuEZhiFuVC alloc]init];
            yuevc.block = ^(){
                [self dismissViewControllerAnimated:NO completion:nil];
            };
            //            yuevc.modalPresentationStyle = UIModalTransitionStyleCoverVertical;
            yuevc.order_id = _order_id;
            yuevc.sum_money =_totalMoey;
            [self presentViewController:yuevc animated:YES completion:nil];
            return;

            break;
        }
        default:
            break;
    }
}
-(void)payByUPPayPlugin
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:@"1" forKey:@"keytype"];
    [params setObject:_order_id forKey:@"keyid"];
    [params setObject:[NSString stringWithFormat:@"%@00",_totalMoey] forKey:@"total_fee"];
    [WXDataService requestWithURL:@"sys_plugins/onlinepay/unionpay/getTn.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
        NSLog(@"银联接口 = %@ - %@ - %@", success, msg, result);
        if ([success boolValue] == 1) {
            NSString *tn = [NSString stringWithFormat:@"%@", [result objectForKey:@"tn"]];
            [UPPayPlugin startPay:tn mode:@"00" viewController:self delegate:self];
//           [self UPPayPluginResult:msg];
        }
    }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

-(void)UPPayPluginResult:(NSString*)result
{
    if ([result isEqualToString:@"success"]) {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:USER_ACCOUNT forKey:@"agency_id_"];
        [params setObject:_totalMoey forKey:@"total_fee"];
        [params setObject:@(flag) forKey:@"type"];
        [WXDataService requestWithURL:@"mobile/agency/rechargeBalance.jhtml" params:params httpMethod:@"POST" block:^(id result, NSString *success, NSString *msg) {
//            UIAlertView *deleAlertView=[[UIAlertView alloc]initWithTitle:nil message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
//            [deleAlertView show];
            [BackView addToview:self.view WithDeleget:self WithMessage:msg WithBtnCount:1];
            return;
        }failblock:^(AFHTTPRequestOperation *operation, NSError *error) {
            
        }];
        return;
    }else if ([result isEqualToString:@"false"]){
        [BackView addToview:self.view WithDeleget:self WithMessage:@"支付失败" WithBtnCount:1];
        return;
    }else if ([result isEqualToString:@"cancel"]){
        [BackView addToview:self.view WithDeleget:self WithMessage:@"已取消支付" WithBtnCount:1];

        return;
    }
}
#pragma mark - 点击红点
-(void)clickBtn:(UIButton *)sender{
    for(int i=0;i<5;i++){
        UIButton *btn = (UIButton *)[self.view viewWithTag:i+1];
        btn.backgroundColor = [UIColor grayColor];
    }
    UIButton *btn = (UIButton *)[self.view viewWithTag:sender.tag - 9];
    btn.backgroundColor = DRINK_RED_COLOR;
    flag = btn.tag;
}
#pragma mark - nav
-(void)loadNav{
    self.view.backgroundColor = DRINK_LIGHTGRAY_COLOR;
    UIView *redview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 20)];
    redview.backgroundColor = DRINK_RED_COLOR;
    [self.view addSubview:redview];
    UILabel *redLab = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, ScreenW, 40)];
    redLab.backgroundColor = DRINK_RED_COLOR;
    redLab.text = @"支付方式";
    redLab.textColor = [UIColor whiteColor];
    redLab.textAlignment =  NSTextAlignmentCenter;
    redLab.font = [UIFont boldSystemFontOfSize:19];
    [self.view addSubview:redLab];
    UIButton *backBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 20,44, 40)];
    backBtn.adjustsImageWhenHighlighted = NO;
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBtn];
}
-(void)clickBack{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
